
package thinkinginjava.concurrency;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class SleepingTask extends LiftOff {

    @Override
    public void run() {
        try {
            while (countDown-- > 0) {
                System.out.println(status());
                int sleepTime = new Random().nextInt(10)+1;
                TimeUnit.SECONDS.sleep(sleepTime);
                System.out.println("#"+id+" has sleep "+sleepTime);
            }
        } catch (InterruptedException e) {
            System.out.println("Interrupted! ");
        }

    }

    public static void main(String[] args) {

        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++)
            exec.execute(new SleepingTask());

        exec.shutdown();

    }

}
